﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.下单MTO.Service;
using DXApplicationTangche.Utils;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.UC.客户CRM.Service
{
    public class CRM客户Service
    {
        /// <summary>
        /// 查询全部客户
        /// </summary>
        /// <returns></returns>
        public static List<CustomerPDTO> findAllCustomers()
        {
            return
                FunctionHelper.JsonDeserialization<List<CustomerPDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/findAllCustomers"
                        )
                    );
        }
        /// <summary>
        /// 根据电话查客户地址
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static CustomerPDTO getCustomerPByMobile(String mobile)
        {
            return
                FunctionHelper.JsonDeserialization<CustomerPDTO>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerPByMobile?mobile=" + mobile)
                    );
        }
        /// <summary>
        /// 根据电话查询客户带客户认证信息和积分
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public static CustomerPWithIdentityAndScoreDTO getCustomerPWithIdentityAndScoreByMobile(String mobile)
        {
            return
                FunctionHelper.JsonDeserialization<CustomerPWithIdentityAndScoreDTO>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerPWithIdentityAndScoreByMobile?mobile=" + mobile)
                    );
        }

        /// 根据条码找客户信息
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public static CustomerPDTO getCustomerIntoByBarcode(String barcode)
        {
            return
                FunctionHelper.JsonDeserialization<CustomerPDTO>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/saleConfirm/getCustomerIntoByBarcode?barcode=" + barcode)
                    );
        }

        /// <summary>
        /// 保存会员
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public static String saveMembership(String mobile, String membershipType)
        {
            return
                RestCall.httpGetMethod(
                    URL.接口url + "/customer/saveMembership?mobile=" + mobile + "&membershipType=" + membershipType
                );
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="customerInfoCreateDTO"></param>
        /// <returns></returns>
        public static String createCustomerInfo(CustomerInfoCreateDTO customerInfoCreateDTO)
        {
            return
                RestCall.PostHttp(
                    URL.接口url + "/customer/createCustomerInfo",
                    FunctionHelper.JsonSerialization(customerInfoCreateDTO)
                );
        }
        /// <summary>
        /// 修改客户量体值
        /// </summary>
        /// <param name="customerInfoCreateDTO"></param>
        /// <returns></returns>
        public static String modifyCustomerFit(int fitCountId, Dictionary<String, String> itemValueAndFitValueMap)
        {
            return
                RestCall.PostHttp(
                    URL.接口url + "/customer/modifyCustomerFit?fitCountId=" + fitCountId,
                    FunctionHelper.JsonSerialization(itemValueAndFitValueMap)
                );
        }

        /// <summary>
        /// 传店铺查客户：坚山、上海坚山、luna
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public static List<CustomerViewShopIdDTO> getCustomerInfoByShopId(String shopId)
        {
            return
                FunctionHelper.JsonDeserialization<List<CustomerViewShopIdDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerInfoByShopId?shopId=" + shopId
                    )
                );
        }
        /// <summary>
        /// 查客户量体：坚山、上海坚山、luna
        /// </summary>
        /// <param name="customerId"></param>
        /// <returns></returns>
        public static List<CustomerFItDTO> getCustomerFitInfoById(int customerId)
        {
            return
                FunctionHelper.JsonDeserialization<List<CustomerFItDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerFitInfoById?customerId=" + customerId
                    )
                );
        }

        /// <summary>
        /// 根据id取用户地址
        /// </summary>
        /// <param name="addressId"></param>
        /// <returns></returns>
        public static CustomerOrderCommitDTO getCustomerAddressById(int addressId)
        {
            return
                FunctionHelper.JsonDeserialization<CustomerOrderCommitDTO>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerAddressById?addressId=" + addressId
                    )
                );
        }

        /// <summary>
        /// 根据姓名模糊查询客户清单
        /// </summary>
        /// <param name="addressId"></param>
        /// <returns></returns>
        public static List<CustomerOrderCommitDTO> getCustomerOrderCommitByConsignee(String consignee)
        {
            return
                FunctionHelper.JsonDeserialization<List<CustomerOrderCommitDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/customer/getCustomerOrderCommitByConsignee?consignee=" + consignee
                    )
                );
        }
        /// <summary>
        /// 根据shop cd查询受托加工收货人地址
        /// </summary>
        /// <param name="shopCd"></param>
        /// <returns></returns>
        public static CustomerOrderCommitDTO getOutSourcingByShopCd(String shopCd)
        {
            return
                FunctionHelper.JsonDeserialization<CustomerOrderCommitDTO>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/outSourcing/getOutSourcingByShopCd?shopCd=" + shopCd
                    )
                );
        }
        /// <summary>
        /// 受托加工收货人地址一览
        /// </summary>
        /// <returns></returns>
        public static List<OutSourcingReceiverAddressDTO> getOutSourcingAll()
        {
            return
                FunctionHelper.JsonDeserialization<List<OutSourcingReceiverAddressDTO>>(
                    RestCall.httpGetMethod(
                        URL.接口url + "/outSourcing/getOutSourcingAll"
                    )
                );
        }
        /// <summary>
        /// 受托加工收货人地址保存
        /// </summary>
        /// <param name="outSourcingReceiverAddressDTO"></param>
        /// <returns></returns>
        public static String saveOutSourcingReceiverAddress(OutSourcingReceiverAddressDTO outSourcingReceiverAddressDTO)
        {
            String xx = FunctionHelper.JsonSerialization(outSourcingReceiverAddressDTO);
            return
            RestCall.PostHttp(
                URL.接口url + "/outSourcing/saveOutSourcingReceiverAddress",
                FunctionHelper.JsonSerialization(outSourcingReceiverAddressDTO)
                );
        }
    }

    public class CustomerOrderCommitDTO
    {
        public String customerId { get; set; }//客户id
        public String customerName { get; set; }//客户名称
        public String mobile { get; set; }  //手机
        public String addressId { get; set; }//地址id
        public String addressProvince { get; set; }//省
        public String addressCity { get; set; }//市
        public String addressDistrict { get; set; }//区
        public String addressDetail { get; set; }//详细地址
        public String zipCode { get; set; }//邮编
        public CustomerOrderCommitDTO()
        {

        }
        public String toString()
        {
            return ValueUtil.StringNullValue(this.customerName) 
                                    + ValueUtil.StringNullValue(this.mobile)
                                    + ValueUtil.StringNullValue(this.addressProvince)
                                    + ValueUtil.StringNullValue(this.addressCity)
                                    + ValueUtil.StringNullValue(this.addressDistrict)
                                    + ValueUtil.StringNullValue(this.addressDetail)
                                    + ValueUtil.StringNullValue(this.zipCode);
        }
    }

    public class CustomerFItDTO
    {
        public int id { get; set; }
        public int customerId { get; set; }
        public String customerName { get; set; }
        public String sex { get; set; }
        public int age { get; set; }
        public String styleCategoryCd { get; set; }
        public String itemCd { get; set; }
        public String itemValue { get; set; }
        public String fitValue { get; set; }
        public String propertyNameCn { get; set; }
        public String itemNameCn { get; set; }
        public CustomerFItDTO()
        {

        }
    }
    public class CustomerViewShopIdDTO
    {
        public int customerId { get; set; }
        public String name { get; set; }
        public String mobile { get; set; }
        public DateTime createDate { get; set; }
        public int addressId { get; set; }  //  地址id
        public String addressName { get; set; }
        public int fitId { get; set; }  //  量体id
        public String fitName { get; set; }
        public CustomerViewShopIdDTO()
        {

        }
    }

    public class CustomerInfoCreateDTO
    {
        
        /*
*  a_customer_p
*/
        public String customerFirstName { get; set; }
        public String customerLastName { get; set; }
        public String mobile { get; set; }
        public int defaultAddrFlag { get; set; }
        public String sexCd { get; set; }
        public String eMailUrl { get; set; }
        public String customerStatus { get; set; }

        /*
         *  a_customer_fit_count_r
         */
        public String sex { get; set; }

        //  体型、喜好、肩型、胸型。K:量体值项目，V:量体值
        public Dictionary<String, String> itemValueAndFitValueMap { get; set; } = new Dictionary<string, string>();

        //  地址信息
        public CustomerAddressInfoDTO addressDto { get; set; } = new CustomerAddressInfoDTO();
        //  门店客户表
        public SimpleDropDownDTO shopCustomerRelationDto { get; set; } = new SimpleDropDownDTO();

        public CustomerInfoCreateDTO(string customerFirstName, string customerLastName, string mobile, int defaultAddrFlag, string sexCd, string eMailUrl, string customerStatus, string sex, Dictionary<string, string> itemValueAndFitValueMap, CustomerAddressInfoDTO addressDto, SimpleDropDownDTO shopCustomerRelationDto)
        {
            this.customerFirstName = customerFirstName;
            this.customerLastName = customerLastName;
            this.mobile = mobile;
            this.defaultAddrFlag = defaultAddrFlag;
            this.sexCd = sexCd;
            this.eMailUrl = eMailUrl;
            this.customerStatus = customerStatus;
            this.sex = sex;
            this.itemValueAndFitValueMap = itemValueAndFitValueMap;
            this.addressDto = addressDto;
            this.shopCustomerRelationDto = shopCustomerRelationDto;
        }

    }

    public class CustomerAddressInfoDTO
    {
        public String addressId { get; set; }
        public String consignee { get; set; }    //  收货人
        public String addressProvince { get; set; } //  省
        public String addressCity { get; set; } //  市
        public String addressDistrict { get; set; }    //  县
        public String addressDetail { get; set; }  //  详细地址
        public String zip_code { get; set; }   //  邮编
        public String mobile { get; set; }  //  联系人电话

        public CustomerAddressInfoDTO()
        {

        }

        public CustomerAddressInfoDTO(string consignee, string addressProvince, string addressCity, string addressDistrict, string addressDetail, string zip_code, string mobile)
        {
            this.consignee = consignee;
            this.addressProvince = addressProvince;
            this.addressCity = addressCity;
            this.addressDistrict = addressDistrict;
            this.addressDetail = addressDetail;
            this.zip_code = zip_code;
            this.mobile = mobile;
        }
    }

    public class SimpleDropDownDTO
    {
        public String id { get; set; }
        public String name { get; set; }
        public SimpleDropDownDTO()
        {

        }

        public SimpleDropDownDTO(string id)
        {
            this.id = id;
        }
    }
    public class OutSourcingReceiverAddressDTO
    {
        public String addressDetail { get; set; }
        public String city { get; set; }
        public String contact { get; set; }
        public String country { get; set; }
        public String county { get; set; }
        public String mobile { get; set; }
        public String province { get; set; }
        public String shopCd { get; set; }
        public String tag { get; set; }
        public OutSourcingReceiverAddressDTO()
        {

        }

        public OutSourcingReceiverAddressDTO(string shopCd, string country, string tag, string contact, string mobile, string province, string city, string county, string addressDetail)
        {
            if (!String.IsNullOrEmpty(addressDetail))
                this.addressDetail = addressDetail;
            else
                throw new Exception("详细地址为空");
            if (!String.IsNullOrEmpty(city))
                this.city = city;
            else
                throw new Exception("城市为空");
            if (!String.IsNullOrEmpty(contact))
                this.contact = contact;
            else
                throw new Exception("收货人姓名为空");
            if (!String.IsNullOrEmpty(country))
                this.country = country;
            else
                throw new Exception("国家为空");
            if (!String.IsNullOrEmpty(county))
                this.county = county;
            else
                throw new Exception("区为空");
            if (!String.IsNullOrEmpty(mobile))
                this.mobile = mobile;
            else
                throw new Exception("电话为空");
            if (!String.IsNullOrEmpty(province))
                this.province = province;
            else
                throw new Exception("省为空");
            if (!String.IsNullOrEmpty(shopCd))
                this.shopCd = shopCd;
            else
                throw new Exception("店铺编码为空");
            if (!String.IsNullOrEmpty(tag))
                this.tag = tag;
            else
                throw new Exception("店铺名称为空");
        }

        public OutSourcingReceiverAddressDTO build(string country, string tag, string contact, string mobile, string province, string city, string county, string addressDetail) {
            if (!String.IsNullOrEmpty(addressDetail))
                this.addressDetail = addressDetail;
            else
                throw new Exception("详细地址为空");
            if (!String.IsNullOrEmpty(city))
                this.city = city;
            else
                throw new Exception("城市为空");
            if (!String.IsNullOrEmpty(contact))

                this.contact = contact;
            else
                throw new Exception("收货人姓名为空");
            if (!String.IsNullOrEmpty(country))
                this.country = country;
            else
                throw new Exception("国家为空");
            if (!String.IsNullOrEmpty(county))
                this.county = county;
            else
                throw new Exception("区为空");
            if (!String.IsNullOrEmpty(mobile))
                this.mobile = mobile;
            else
                throw new Exception("电话为空");
            if (!String.IsNullOrEmpty(province))
                this.province = province;
            else
                throw new Exception("省为空");
            if (!String.IsNullOrEmpty(tag))
                this.tag = tag;
            else
                throw new Exception("店铺名称为空");
            return this;
        }
    }
}
